package com.skycode7.dao;

import com.skycode7.bean.AdminBean;
import com.skycode7.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    DBConnection DBConn = new DBConnection();

    public boolean CheckLogin(String s1, String s2) throws SQLException {
        Connection conn = DBConn.getConn();//获取Mysql链接
        Statement statement = conn.createStatement();//获取
        ResultSet rs = null;//声明结果集对象
        boolean OK = true;
        String sql = "select * from admin where AdminName ='" + s1 + "' and AdminPwd = '" + s2 + "'";//查询账户密码语句
        rs = statement.executeQuery(sql);
        if (!rs.next()) {
            OK = false;
        } else {
            OK = true;
            //todo 后期需要添加账户密码严格验证
        }
        return OK;
    }
    public int add(AdminBean admin){
        Connection conn = DBConn.getConn();//conn代表数据库链接
        String sql = "insert into notice values(null,?,?,?,?)";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,admin.getAdminName());
            ps.setString(2,admin.getAdminPwd());
            ps.setInt(3,admin.getAdminType());
            ps.setString(4,admin.getLastLoginTime());
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("插入news表异常");
        }
        return 0;
    }
    public int delete (int AdminID){
        Connection conn = DBConn.getConn();//conn代表数据库链接

        String sql = "delete from admin where AdminID=?";
        try {
            PreparedStatement ps =  conn.prepareStatement(sql);
            ps.setInt(1,AdminID);
        } catch (SQLException e) {
            System.out.println("删除news表异常");
        }
        return 0;
    }
    public int update(AdminBean admin){
        Connection conn = DBConn.getConn();//conn代表数据库链接
        String sql = "update admin set AdminName=?,AdminPwd=?,AdminType=?,LastLoginTime=? where AdminID=?";//使用jdbc预处理对象添加
        try {
            PreparedStatement ps =  conn.prepareStatement(sql);
            ps.setString(1,admin.getAdminName());
            ps.setString(2,admin.getAdminPwd());
            ps.setInt(3,admin.getAdminType());
            ps.setString(4,admin.getLastLoginTime());
            ps.setInt(5,admin.getAdminID());
            return ps.executeUpdate();

        } catch (SQLException e) {
            System.out.println("更新notice表异常");
        }
        return 0;
    }
    public List<AdminBean> getList(int page){
        Connection conn = DBConn.getConn();//conn代表数据库链接
        String sql = "select *from admin limit ?,?";//第一个开始的行数 第二个是查询显示的条数
        List<AdminBean> list = new ArrayList<>();
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,(page-1)*10);//page为1是行数为0 page为2时 10行
            ps.setInt(2,10);// 10 表示10条数
            ResultSet rs = ps.executeQuery();//rs 是数据查询结果集 全部是字符串
            while (rs.next()){
                AdminBean admin =new AdminBean();//每轮循环生命一个新闻对象
                admin.setAdminID(rs.getInt("AdminID"));//rs 获取该行新闻ID 通过新闻对象封装方法设置新闻值notices .setNewsTitle(rs.getString("NewsTitle"));
                admin.setAdminName(rs.getString("AdminName"));
                admin.setAdminPwd(rs.getString("AdminPwd"));
                admin.setAdminType(rs.getInt("AdminType"));
                admin.setLastLoginTime(rs.getString("LastLoginTine"));
                list.add(admin);//将设置好的新闻对象放入list中
            }
        } catch (SQLException e) {
            System.out.println("查询news表异常");
        }
        return list;//如果添加异常返回空
    }
}

